package com.api.oa.mapper;

import com.api.oa.model.entity.YQPatient;
import com.api.oa.model.vo.OaNoticeVo;
import com.api.oa.model.vo.YQPatientVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface YQPatientMapper {

    @Insert("insert into  yq_patient  (" +
        "patientPhone," +
        "patientIdCard," +
        "patientName," +
        "patientSex," +
        "patientFace," +
        "IdCardType," +
        "patientAddress," +
        "lat," +
        "lng," +
        "patientIns," +
        "patientAddBed," +
        "patientType," +
        "patientLevel," +
        "attendType," +
        "disabled," +
        "conditionDesc" +
    ") " +
    "values " +
    "(" +
        "#{entity.patientPhone}," +
        "#{entity.patientIdCard}," +
        "#{entity.patientName}," +
        "#{entity.patientSex}," +
        "#{entity.patientFace}," +
        "#{entity.IdCardType}," +
        "#{entity.patientAddress}," +
        "#{entity.lat}," +
        "#{entity.lng}," +
        "#{entity.patientIns}," +
        "#{entity.patientAddBed}," +
        "#{entity.patientType}," +
        "#{entity.patientLevel}," +
        "#{entity.attendType}," +
        "#{entity.disabled}," +
        "#{entity.conditionDesc}" +
    ")" )
    //@Options(useGeneratedKeys = true, keyProperty = "noticeId", keyColumn = "id")
    Integer insert(@Param("entity") YQPatient entity);



    @Update("update  yq_patient  set " +
        "patientPhone = #{entity.patientPhone}," +
        "patientIdCard = #{entity.patientIdCard}," +
        "patientName = #{entity.patientName}," +
        "patientSex = #{entity.patientSex}," +
        "patientFace = #{entity.patientFace}," +
        "IdCardType = #{entity.IdCardType}," +
        "patientAddress = #{entity.patientAddress}," +
        "lat = #{entity.lat}," +
        "lng = #{entity.lng}," +
        "patientIns = #{entity.patientIns}," +
        "patientAddBed = #{entity.patientAddBed}," +
        "patientType = #{entity.patientType}," +
        "patientLevel = #{entity.patientLevel}," +
        "attendType = #{entity.attendType}," +
        "disabled = #{entity.disabled}," +
        "conditionDesc = #{entity.conditionDesc}" +
    " where patientIdCard = #{entity.patientIdCard}")
    Integer update(@Param("entity") YQPatient  entity);


    @Delete("delete from yq_patient where patientIdCard=#{id}")
    void delete(@Param("id") String id);


    @Select("select * from  yq_patient  where noticeId = #{pid}")
    List<YQPatient> list(@Param("id") Long pid);


    @Select("<script>select * from  v_yq_patient_user <where> 1 = 1 " +
            "<if test='id!=null and id!=\"\"'> and concat_ws(ifnull(patientIdCard,''),ifnull(patientName,''),ifnull(patientPhone,''))  like concat('%',#{id},'%') </if> " +
            "<if test='insCode!=null and insCode!=\"\"'> and patientIns = #{insCode} </if> " +
            "<if test='attendType!=null and attendType!=\"\"'> and attendType = #{attendType} </if></where> </script>")
    List<YQPatientVO> page(@Param("id") String patientIdCard, @Param("insCode") String insCode,@Param("attendType") String attendType);


    @Select("select * from v_yq_patient_user  where userId = #{id} limit 1")
    YQPatient findOne(@Param("id") Long id);


    @Select("select * from  yq_patient  where patientPhone = #{patientPhone} limit 1")
    YQPatient findOneByPhone(@Param("patientPhone") String patientPhone);


    @Select("select * from  yq_patient  where patientIdCard = #{patientIdCard} limit 1")
    YQPatient findOneByIdCard(@Param("patientIdCard") String patientIdCard);


    @Update("update  yq_patient  set " +
            "lat = #{lat}," +
            "lng = #{lng}" +
            " where patientIdCard = #{patientIdCard}")
    Integer updateLatLngByUserId(@Param("patientIdCard") String patientIdCard, @Param("lat") Double lat, @Param("lng") Double lng);



    @Select("<script>select * from  v_yq_patient_user <where> 1 = 1 " +
            "<if test='insCode!=null and insCode!=\"\"'> and patientIns = #{insCode} </if> " +
            "<if test='keyWord!=null and keyWord!=\"\"'> and concat(ifnull(patientName,''),ifnull(patientIdCard,'')) like concat('%',#{keyWord},'%') </if> " +
            "<if test='attendType!=null and attendType!=\"\"'> and attendType = #{attendType} </if>" +
            "</where> </script>")
    List<YQPatientVO> pageByIns( @Param("insCode") String belongInsCode, @Param("attendType") String attendType,@Param("keyWord") String keyWord);


    @Select("<script>select v_yq_patient_user.lat,v_yq_patient_user.lng,v_yq_patient_user.userId,v_yq_patient_user.patientName from  v_yq_patient_user <where> 1 = 1 " +
            "<if test='insCode!=null and insCode!=\"\"'> and patientIns = #{insCode} </if> " +
            "</where> </script>")
    List<YQPatientVO> allPatient(String insCode);


    @Select("<script> select count(*) from yq_patient where 1=1 <if test='insCode!=null and insCode!=\"\"'> and patientIns = #{insCode}</if> </script>")
    Long count(@Param("insCode") String insCode);


    @Select("<script>select count(*) from yq_patient where patientSex = 1 <if test='insCode!=null and insCode!=\"\"'> and patientIns = #{insCode}</if> </script>")
    Long countMale(@Param("insCode")String insCode);


    @Select("<script> select count(*) from yq_patient where patientSex = 0 <if test='insCode!=null and insCode!=\"\"'> and patientIns = #{insCode}</if> </script>")
    Long countFeMale(@Param("insCode") String insCode);

}

